using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class BackgroundSoundManager : MonoBehaviour
{
	public static BackgroundSoundManager instance;

	public AudioMixerGroup carMixerGroup;

	public MusicId currentState;

	public ZoneId currentZone = ZoneId.city;

	public SnapshotId lastSfxSnapshot;

	public MusicInfo infoCurMusic;

	public GameObject objCurMusic;

	public List<BackGroundAudio> listMusic = new List<BackGroundAudio>();

	public List<SfxSnapshot> listSnapshot = new List<SfxSnapshot>();

	private List<ZoneMusic> listEnterZone = new List<ZoneMusic>();

	private GameObject objOldMusic;

	private float needPlayChaising;

	private bool chaisingPause;

	private bool explosionActive;

	public bool test;

	private bool isPreloadMusic;

	private void Awake()
	{
		instance = this;
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Start()
	{
		foreach (BackGroundAudio item in listMusic)
		{
			Save.SaveFloat(MusicInfo.keyLastTime + item.namePref, 0f);
		}
		PlayMusic(MusicId.Main);
		SetMainSfxSnapshot();
	}

	private void Update()
	{
		if (test)
		{
			SetSfxSnapshot(SnapshotId.Explosion);
			test = false;
		}
		if (!chaisingPause)
		{
			if (needPlayChaising > 0f)
			{
				needPlayChaising -= Time.deltaTime;
			}
			else if (currentState == MusicId.Attack)
			{
				PlayMain();
			}
		}
		if (GameController.instance.playerScript == null)
		{
			return;
		}
		if (GameController.instance.playerScript.inCar || GameController.instance.playerScript.inHelic)
		{
			if (currentState != MusicId.Car)
			{
				PlayMain();
			}
		}
		else if (currentState == MusicId.Car)
		{
			PlayMain();
		}
	}

	private bool IsPause()
	{
		if (Time.timeScale.Equals(0f))
		{
			return true;
		}
		if (PauseWindow.instance.isVisible)
		{
			return true;
		}
		return false;
	}

	private BackGroundAudio GetMusic(MusicId needMusic)
	{
		BackGroundAudio backGroundAudio = listMusic.Find((BackGroundAudio nameMusic) => (nameMusic.state == needMusic && nameMusic.typeGame == TypeGame.deathmath) || (nameMusic.state == needMusic && nameMusic.typeGame != TypeGame.deathmath && nameMusic.typeGame == GameController.instance.curTypeGame));
		if (backGroundAudio != null)
		{
			return backGroundAudio;
		}
		return null;
	}

	public void PlayMusic(MusicId needMusic, float forceTime = -1f)
	{
		if (needMusic != currentState && !isPreloadMusic)
		{
			currentState = needMusic;
			StartCoroutine(PreloadMusic(GetMusic(needMusic), forceTime));
		}
	}

	private IEnumerator PreloadMusic(BackGroundAudio needMusic, float forceTime = -1f)
	{
		isPreloadMusic = true;
		if (needMusic != null)
		{
			ResourceRequest reqLoad = Resources.LoadAsync("Background_Musik/" + needMusic.namePref);
			while (!reqLoad.isDone)
			{
				yield return null;
			}
			GameObject nMusicObj2 = reqLoad.asset as GameObject;
			nMusicObj2 = Object.Instantiate(nMusicObj2);
			nMusicObj2.transform.parent = base.transform;
			nMusicObj2.transform.name = needMusic.namePref;
			infoCurMusic = nMusicObj2.GetComponent<MusicInfo>();
			if (objOldMusic != null)
			{
				Object.Destroy(objOldMusic);
			}
			if (objCurMusic != null && infoCurMusic != null)
			{
				DestoyAfterDelay delScr = objCurMusic.AddComponent<DestoyAfterDelay>();
				delScr.StartTimer(infoCurMusic.time);
				objOldMusic = objCurMusic;
			}
			objCurMusic = nMusicObj2;
			infoCurMusic.Play(forceTime);
			isPreloadMusic = false;
		}
	}

	public void PlayMain(float forceTime = -1f)
	{
		if (explosionActive && currentState != MusicId.Explosion)
		{
			return;
		}
		if (needPlayChaising > 0f)
		{
			if (currentState != 0)
			{
				PlayChaising(needPlayChaising);
			}
			return;
		}
		if (IsPause())
		{
			instance.PlayMusic(MusicId.Pause, forceTime);
			return;
		}
		switch (currentZone)
		{
		case ZoneId.none:
			instance.PlayMusic(MusicId.none, forceTime);
			break;
		case ZoneId.city:
			instance.PlayMusic(MusicId.Main, forceTime);
			break;
		case ZoneId.zomby:
			instance.PlayMusic(MusicId.Zombie, forceTime);
			break;
		}
	}

	public void PlayChaising(float timePlay = 5f)
	{
		if (needPlayChaising < timePlay)
		{
			needPlayChaising = timePlay;
		}
		if (!explosionActive)
		{
			instance.PlayMusic(MusicId.Attack);
		}
	}

	public void ResetChaising()
	{
		needPlayChaising = 0f;
		PlayMain();
	}

	public void EnterZone(ZoneMusic needZone)
	{
		if (!listEnterZone.Contains(needZone))
		{
			listEnterZone.Add(needZone);
		}
		currentZone = needZone.idZone;
		PlayMain();
	}

	public void ExitZone(ZoneMusic needZone)
	{
		listEnterZone.Remove(needZone);
		if (listEnterZone.Count > 0)
		{
			currentZone = listEnterZone[listEnterZone.Count - 1].idZone;
		}
		else
		{
			currentZone = ZoneId.city;
		}
		PlayMain();
	}

	public SfxSnapshot GetSfxSnap(SnapshotId val)
	{
		SfxSnapshot sfxSnapshot = listSnapshot.Find((SfxSnapshot idSfx) => idSfx.id == val);
		if (sfxSnapshot != null)
		{
			return sfxSnapshot;
		}
		return null;
	}

	public void SetSfxSnapshot(SnapshotId val, float timePlay = -1f)
	{
		SfxSnapshot sfxSnap = GetSfxSnap(val);
		if (sfxSnap != null && sfxSnap.snap != null)
		{
			float num = 0f;
			num = ((!(timePlay > 0f)) ? sfxSnap.time : timePlay);
			sfxSnap.snap.TransitionTo(num);
			lastSfxSnapshot = val;
			AddEffectSnapshot(val);
		}
	}

	public void SetMainSfxSnapshot()
	{
		if (!explosionActive && lastSfxSnapshot != SnapshotId.Main)
		{
			SetSfxSnapshot(SnapshotId.MainSfx);
		}
	}

	public void AddEffectSnapshot(SnapshotId val)
	{
		switch (val)
		{
		case SnapshotId.Main:
		case SnapshotId.MainSfx:
			PlayMain();
			break;
		case SnapshotId.Pause:
			break;
		case SnapshotId.Explosion:
			explosionActive = true;
			CancelInvoke("ResetExplosion");
			Invoke("ResetExplosion", 2f);
			PlayMusic(MusicId.Explosion);
			break;
		case SnapshotId.UndeWater:
			break;
		}
	}

	private void ResetExplosion()
	{
		explosionActive = false;
		CancelInvoke("ResetExplosion");
		SetMainSfxSnapshot();
	}
}
